#!/bin/bash

# For packaging the compute system up.
#

# Filter out re-routable libraries
export PRIMARY="$1"
export ADDITION_SELF="-C `dirname $1` `basename $1`"
export ADDITION_LIST="$(ldd $1 | grep '=>' | grep -v '=>  ' | grep -v "libc\." | grep -v "libm\." | grep -v "libdl\." | grep -v "libstdc++\." | awk ' { print $3 } ')"
if [ $# -gt 2 ]; then
   export ADDITION_LIST="$ADDITION_LIST \
   $(ldd $3 | grep '=>' | grep -v '=>  ' | grep -v "libc\." | grep -v "libm\." | grep -v "libdl\." | grep -v "libstdc++\." | awk ' { print $3 } ')"
fi
mkdir -p temp_ldd_wrap_$1
cd temp_ldd_wrap_$1
for line in $ADDITION_LIST
do
   cp $line ./
done
export TARARGS="$ADDITION_SELF"
export TARARGS="$TARARGS -C `dirname $2` glibc_patch.c glibc_patch_version.map.c binary_patch.sh"
while test $# -gt 2
do
   if [ ! -f $3 ]; then
      cp `which $3` ./
   else
      cp $3 ./
   fi
   shift
done
chmod +w *
for line in *
do
   export TARARGS="$TARARGS -C $PWD `basename $line`"
done
cd ..
rm $PRIMARY.tar.gz
tar chvzf $PRIMARY.tar.gz $TARARGS
rm -fr temp_ldd_wrap_$PRIMARY
